package com.admin.dao.librarydao;

import com.admin.dto.BookListBean;
import com.admin.dto.BookTopListBean;
import com.admin.dto.UserSendBean;
import com.pojo.BorrowerBean;
import com.pojo.UserSendMsgBean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LibraryPageImpl implements LibraryPageDaoInF{


    @Override
    public List<UserSendBean> getUserMsgInfo(String sql, Connection conn) throws SQLException {
        List<UserSendBean> list = new ArrayList<>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        UserSendBean borObj = null;

        try {
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                borObj = new UserSendBean();
                borObj.setUsername(rs.getString("username"));
                borObj.setMsg(rs.getString("msg"));
                borObj.setMsgsendtime(rs.getString("msgsendtime"));
                list.add(borObj);
            }
        } finally {
            if (pstm != null) {
                pstm.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        return list;
    }
    //暂时不要
    @Override
    public String getUserNameInfo(String sql, Connection conn, Long userid) throws SQLException {

        BorrowerBean borrowerBean=null;
        PreparedStatement pstm = null;
        ResultSet resultSet = null;

        try {
            pstm = conn.prepareStatement(sql);
            pstm.setLong(1,userid);

            resultSet = pstm.executeQuery();
            while (resultSet.next()) {
                borrowerBean = new BorrowerBean();
                borrowerBean.setUserName(resultSet.getString("username"));
            }
        } finally {
            if (pstm != null) {
                pstm.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
        }
        return borrowerBean.getUserName();

    }

    @Override
    public List<BookTopListBean> BookTopInfo(String sql, Connection conn) throws SQLException {

        List<BookTopListBean> list = new ArrayList<>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        BookTopListBean borObj = null;

        try {
            pstm = conn.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                borObj = new BookTopListBean();
                borObj.setBookName(rs.getString("bookname"));
                borObj.setBookSales(rs.getInt("sales"));

                list.add(borObj);
            }
        } finally {
            if (pstm != null) {
                pstm.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        return list;

    }
}
